package com.b2j.language;

import com.b2j.base.Lexema;
import com.b2j.base.MainTools;
import com.b2j.base.Routine;
import com.b2j.base.Token;
import com.b2j.base.TokenGroup;

public class From extends Routine {

	@Override
	public String generate(Lexema lexema) {

		String line = "";
		String variable = "";
		String initialization = "";
		String to = "";
		String step = "";
		lexema = MainTools.tools.nextLexema();
		if (lexema.tokenType == Token.ID) {
			
			variable = lexema.lexema;
			initialization = MainTools.tools.processCodeUntil(lexema, Token.TO);
			
			lexema = MainTools.tools.nextLexema();
			while (lexema.tokenType != Token.STEP && lexema.tokenType != Token.SEMICOLON) {
				if (lexema.tokenType == Token.ID || lexema.group == TokenGroup.LANGUAGE_COMPONENT) 
					to = to + MainTools.tools.identifier(lexema);
				else
					to = to + lexema.lexema;
				lexema = MainTools.tools.nextLexema();
			}
			
			if (lexema.tokenType == Token.STEP) {

				lexema = MainTools.tools.nextLexema();
				while (lexema.tokenType != Token.SEMICOLON) {
					if (lexema.tokenType == Token.ID || lexema.group == TokenGroup.LANGUAGE_COMPONENT) 
						step = step + MainTools.tools.identifier(lexema);
					else
						step = step + lexema.lexema;

					lexema = MainTools.tools.nextLexema();
				}

			}
			
			if (step.isEmpty())
				step = "1";
			
			line = "for(" + variable + initialization + ";" + variable + "<=" + to + ";" + variable + "+=" + step + "){\n";
			
			line = line + MainTools.tools.processCodeUntil(lexema, Token.END);
			line = line + "}\n";
			
		}
		
		return line;
	}



}
